-- https://wezfurlong.org/wezterm/index.html
-- https://github.com/QianSong1/wezterm-config/
local wezterm = require 'wezterm'

local launch_menu = {}
local default_prog = {}
local set_environment_variables = {}
local act = wezterm.action 

require('events.right-status').setup()
require('events.tab-title').setup()
require('events.new-tab-button').setup()

-- 登录Shell
if wezterm.target_triple == 'x86_64-pc-windows-msvc' then
    -- window
    table.insert( launch_menu, {
        label = 'Git Bash',
        args = { 'bash.exe', '-l'}
     } )
    table.insert( launch_menu, {
        label = 'WSL',
        args = { 'wsl.exe', '--cd', "/home/" }
     } )
    default_prog = { 'bash.exe', '-l' }
elseif wezterm.target_triple == 'x86_64-unknown-linux-gnu' then
    -- linux，当前进入此分支
    table.insert( launch_menu, {
        label = 'Zsh1',
        args = { 'zsh', '-l' }
     } )
    default_prog = { 'zsh', '-l' }
else
    table.insert( launch_menu, {
        label = 'Zsh2',
        args = { 'zsh', '-l' }
     } )
    default_prog = { 'zsh', '-l' }
end

-- Title标题
function basename( s )
    return string.gsub( s, '(.*[/\\])(.*)', '%2' )
end

wezterm.on( 'format-tab-title', function( tab, tabs, panes, config, hover, max_width )
    local pane = tab.active_pane

    local index = ""
    if #tabs > 1 then
        index = string.format( "%d: ", tab.tab_index + 1 )
    end
    
    local process = basename( pane.foreground_process_name )

    return { {
        Text = ' ' .. index .. process .. ' '
     } }
end )

-- Startup
wezterm.on( 'gui-startup', function( cmd )
    local tab, pane, window = wezterm.mux.spawn_window( cmd or {} )
    window:gui_window():maximize()
end )


local config = {
    -- Basic基本配置
    -- 不检查更新
    check_for_updates = false,
    -- 关闭tab页时回到最后一次激活的tab窗口
    switch_to_last_active_tab_when_closing_tab = true,
    enable_scroll_bar = true,

    -- Window窗口配置
    native_macos_fullscreen_mode = true,
    adjust_window_size_when_changing_font_size = true,

    -- window
    window_decorations = "INTEGRATED_BUTTONS|RESIZE",
    integrated_title_button_style = "Windows",
    integrated_title_button_color = "auto",
    integrated_title_button_alignment = "Right",
    initial_cols = 120,
    initial_rows = 24,
    -- 窗口透明度
    -- window_background_opacity = 0.9,
    -- 窗口边距
    window_padding = {
        left = 5,
        right = 5,
        top = 5,
        bottom = 1
     },
     window_close_confirmation = 'AlwaysPrompt',
     window_frame = {
         active_titlebar_bg = '#090909',
     },
    -- 非激活状态的面板亮度变暗
    inactive_pane_hsb = {
        --hue = 1.8,
        saturation = 1.0,
        brightness = 0.3 
    },
    -- 背景图片
    window_background_image = '/home/kevin/Pictures/scrrenbg/3.jpg',
    window_background_image_hsb = {
        -- 明亮度
        brightness = 0.1,
        -- 色彩
        hue = 1.0,
        -- 饱和度
        saturation = 1.0
     },

    -- Font字体配置
    --font = wezterm.font_with_fallback { 'Fira Code' },
    font = wezterm.font {
        family = 'JetBrains Mono',
        harfbuzz_features = { 'calt=0', 'clig=0', 'liga=0' }
     },
    font_size = 14,
    -- freetype_load_target = "Mono",

    -- Tab bar
    -- enable_tab_bar = true,
    -- hide_tab_bar_if_only_one_tab = false,
    -- show_tab_index_in_tab_bar = false,
    -- 在底部显示Tab栏
    -- tab_bar_at_bottom = false,
    -- tab_max_width = 25,

    -- Keys
    disable_default_key_bindings = true,
    -- 设置Leader键
    leader = { key = 'b', mods = 'CTRL' },

    -- Allow using ^ with single key press.
    use_dead_keys = false,

    keys = {  -- New/close pane
        -- 全屏
        { key = 'Enter', mods = 'ALT', action = act.ToggleFullScreen }, 

        -- 快速粘贴
        { key = 'Insert', mods = 'SHIFT', action = act.PasteFrom 'Clipboard' }, 

        -- 新建窗口
        { key = 'c', mods = 'CTRL|SHIFT', action = act.SpawnTab 'CurrentPaneDomain' }, 
        { key = 't', mods = 'CTRL|SHIFT', action = act.SpawnTab 'CurrentPaneDomain' }, 
        -- 关闭窗口
        { key = 'x', mods = 'CTRL|SHIFT', action = act.CloseCurrentTab { confirm = true } }, 
        -- Tab navigation 窗口间移动和切换
        -- 左移窗口
        { key = 'h', mods = 'CTRL|SHIFT', action = act { ActivateTabRelative = -1 } }, 
        -- 右移窗口
        { key = 'l', mods = 'CTRL|SHIFT', action = act { ActivateTabRelative = 1 } }, 

        -- Split 分隔面板
        { key = '-', mods = 'ALT', action = act.SplitVertical { domain = 'CurrentPaneDomain' } }, 
        { key = '\\', mods = 'ALT', action = act.SplitHorizontal { domain = 'CurrentPaneDomain' } }, 
        -- 面板缩放状态
        { key = 'z', mods = 'ALT', action = 'TogglePaneZoomState' }, 
        -- 关闭面板
        { key = 'x', mods = 'ALT', action = act.CloseCurrentPane { confirm = true } }, 

        -- Pane navigation 面板间移动
        -- 左移
        { key = 'h', mods = 'ALT', action = act.ActivatePaneDirection 'Left' }, 
        -- 下移
        { key = 'j', mods = 'ALT', action = act.ActivatePaneDirection 'Down' }, 
        -- 上移
        { key = 'k', mods = 'ALT', action = act.ActivatePaneDirection 'Up' }, 
        -- 右移
        { key = 'l', mods = 'ALT', action = act.ActivatePaneDirection 'Right' }, 

        -- Resize 调整面板大小
        { key = 'LeftArrow', mods = 'ALT|SHIFT', action = act { AdjustPaneSize = { 'Left', 5 } } }, 
        { key = 'DownArrow', mods = 'ALT|SHIFT', action = act { AdjustPaneSize = { 'Down', 5 } } }, 
        { key = 'UpArrow', mods = 'ALT|SHIFT', action = act { AdjustPaneSize = { 'Up', 5 } } }, 
        { key = 'RightArrow', mods = 'ALT|SHIFT', action = act.AdjustPaneSize { 'Right', 5 } }, 

        -- Copy/paste buffer
        { key = '[', mods = 'LEADER', action = 'ActivateCopyMode' }, 
        { key = ']', mods = 'LEADER', action = 'QuickSelect' } 
    },

    -- 颜色
    color_scheme = 'Monokai (base16)',
    colors = {
        tab_bar = {
            background = '#ffff00',
            -- The color of the inactive tab bar edge/divider
            inactive_tab_edge = '#0057ff',

            active_tab = {
                bg_color = '#4477cc',
                fg_color = '#ffc0c0',
                intensity = 'Normal',
                underline = 'None',
                italic = false,
                strikethrough = false,
            },
            inactive_tab = {
                bg_color = '#333333',
                fg_color = '#808080',
                -- The same options that were listed under the `active_tab` section above
                -- can also be used for `inactive_tab`.
            },
            inactive_tab_hover = {
                bg_color = '#3b3052',
                fg_color = '#909090',
                italic = true,
            },
            new_tab = {
                bg_color = '#1b1032',
                fg_color = '#808080',
            },
            new_tab_hover = {
                bg_color = '#3b3052',
                fg_color = '#909090',
                italic = true,
            },
        }
    },


    mouse_bindings = {  
        -- Paste on right-click 右击粘贴
        {
            event = { Down = { streak = 1, button = 'Right' } },
            mods = 'NONE',
            action = act { PasteFrom = 'Clipboard' }
        }, 
        -- Change the default click behavior so that it only selects
        -- text and doesn't open hyperlinks
        {
            event = { Up = { streak = 1, button = 'Left' } },
            mods = 'NONE',
            action = act { CompleteSelection = 'ClipboardAndPrimarySelection' }
        }, 
        -- CTRL-Click open hyperlinks 打开超链接
        {
            event = { Up = { streak = 1, button = 'Left' } },
            mods = 'CTRL',
            action = 'OpenLinkAtMouseCursor'
        } 
    },

    default_prog = default_prog,
    set_environment_variables = set_environment_variables,
    launch_menu = launch_menu
}

-- 添加快速切换窗口快捷键
for i = 1, 9 do
    table.insert(config.keys, {
        key = tostring(i),
        mods = 'ALT|SHIFT',
        action = act.ActivateTab(i - 1),
    })
end


return config
